package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.Major;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 专业信息表dao
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public class MajorDaoImp implements CommnonDao{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(Object value) throws Exception {
		//判断类   instanceof 
		if(value instanceof Major){
			String sql="INSERT INTO t_major VALUE (null,?,?,?,?,?);";
			JdbcUtil.update(sql, ((Major) value).getMajorName(),
		                         ((Major) value).getMajorSize(),
		                         ((Major) value).getCreateTime(),
		                         ((Major) value).getMajorType(),
		                         ((Major) value).getMajorState());
			
		}
		return 0;
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(Object value) throws Exception {
		if(value instanceof Major){
			String sql="UPDATE t_major SET major_name=?,major_size =?,";
					sql+="major_type=?,major_state=? WHERE major_id=?;";
			JdbcUtil.update(sql,((Major) value).getMajorName(),
					            ((Major) value).getMajorSize(),
					            ((Major) value).getMajorType(),
					            ((Major) value).getMajorState(),
					            ((Major) value).getMajorId());
		}
		return 0;
	}
	/**
	 * 删除内容
	 * 班级表t_class,课程信息表t_course作业信息表t_homework
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Object value) throws Exception {
		if(value instanceof Major){
			String sql="DELETE t_major,t_homework,t_course,t_class FROM t_major";
                   sql+=" LEFT JOIN t_course ON t_course.major_name=t_major.major_name ";
                   sql+=" LEFT JOIN t_homework ON t_homework.major_name=t_major.major_name";
                   sql+=" LEFT JOIN t_class ON t_class.junior_major_name=t_major.major_name ";
                   sql+=" OR t_class.graduate_major_name=t_major.major_name";
                   sql+=" WHERE t_major.major_id=?;";
			JdbcUtil.update(sql,((Major) value).getMajorId());
		}
		return 0;
	}
	/**
	 * 查找所有的内容
	 * 班级表t_class,课程信息表t_course作业信息表t_homework
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Major> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_major LIMIT ?,?;";
		List<Major> majors=JdbcUtil.query(Major.class, sql,pageSize*(pageNo-1),pageSize);
		return majors;
	}

	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 条件查询
	 * @param key 条件
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Major> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_major WHERE major_id=? OR major_name=? OR major_type=? LIMIT ?,?;";
		List<Major> major=JdbcUtil.query(Major.class, sql, key,key,key,pageSize*(pageNo-1),pageSize);
		return major;
	}
	
	@SuppressWarnings("unchecked")
	public List<Major> findAll(Object key) throws Exception {
		String sql="SELECT * FROM t_major,t_class_info "
				+ "where t_major.major_name=t_class_info.major_name and t_class_info.class_tch_id=?;";
		List<Major> major=JdbcUtil.query(Major.class, sql,key);
		return major;
	}
	
	@Override
	public Object findByKey(Object key) throws Exception {
		return null;
	}
}
